//
//  SDResetPswdViewController.m
//  SKinDiary
//
//  Created by pcyang on 16/7/18.
//  Copyright © 2016年 tencent. All rights reserved.
//

#import "SDResetPswdViewController.h"
#import "SDLoginLTView.h"
#import "SDResetPswdData.h"
#import "SDAccountInfoManager.h"

@interface SDResetPswdViewController()<UITableViewDataSource, UITableViewDelegate,SDFormBaseDataDelegate>
@property(nonatomic,strong)UITableView* tableView;
@property(nonatomic,strong)SDLoginLTView* oldPswdView;
@property(nonatomic,strong)SDLoginLTView* currentPswdView;
@property(nonatomic,strong)SDLoginLTView* confirmPswdView;
@property(nonnull,strong)SDResetPswdData* resetPswdData;
@end

@implementation SDResetPswdViewController
- (void)viewDidLoad
{
    [super viewDidLoad];
    
    [self initSubviews];
    [self initData];
}

-(void)initData
{
    _resetPswdData = [[SDResetPswdData alloc] init];
    _resetPswdData.delegate = self;
}

-(void)initSubviews
{
    BOOL naviHidden = [self navigationBarHidden];
    if (!naviHidden && self.navBar != nil)
    {
        [self.navBar setTitle:@"修改密码"];
        UIImage* backImg = LOAD_IMAGE_USE_CACHE_INBUNDLE(DefaultBundle, @"actionbar_btn_back.png");
        [self.navBar customNaviLeftButton:self action:@selector(onBack:) image:backImg];
        [self.navBar customNaviRightButton:self action:@selector(onDone:) title:@"完成"];
    }
    
    self.automaticallyAdjustsScrollViewInsets = NO;
    
    _tableView = [[UITableView alloc] initWithFrame:autoRectTableViewHaveBar(YES) style:UITableViewStylePlain];
    _tableView.backgroundColor = [UIColor colorWithRGB:0xf9fafc];
    _tableView.dataSource = self;
    _tableView.delegate = self;
    _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    [self.view addSubview:_tableView];
}

- (BOOL)navigationBarHidden
{
    return NO;
}

-(void)dismissKeyboard
{
    [_oldPswdView.textField resignFirstResponder];
    [_currentPswdView.textField resignFirstResponder];
    [_confirmPswdView.textField resignFirstResponder];
}

- (void)showLoading{
    [self finishLoading];
    [VPMBProgressHUD showIndicatorWithText:@"等待中"];
}

- (void)finishLoading
{
    [VPMBProgressHUD hideIndicator];
}

-(UIView*)cretateSeparatorLineView
{
    UIView* separatorLineView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, kScreenWidth, 1)];
    [separatorLineView setBackgroundColor:[UIColor colorWithRGB:0xcad6e3]];
    return separatorLineView;
}


#pragma mark UI Event
- (void)onBack:(id)sender
{
    [self.navigationController popViewControllerAnimated:YES];
    
}

- (void)onDone:(id)sender
{    
    NSString* oldPaswd = [SDAccountInfoManager sharedInstance].password;
    if (![oldPaswd isEqualToString: _oldPswdView.textField.text]) {
        [SDAlertManagement showNormalErrorAlert:@"旧密码不一致"];
        return;
    }

    if (![_currentPswdView.textField.text isEqualToString: _confirmPswdView.textField.text]) {
        [SDAlertManagement showNormalErrorAlert:@"输入的新密码与确认密码不一致"];
        return;
    }
    
    if (CHECK_VALID_STRING(_currentPswdView.textField.text) && CHECK_VALID_STRING(_oldPswdView.textField.text) && CHECK_VALID_STRING(_confirmPswdView.textField.text)) {
        NSString* mobileNumber = [SDAccountInfoManager sharedInstance].mobileNumber;
        
        _resetPswdData.mobileNumber = mobileNumber;
        _resetPswdData.oldPassword = _oldPswdView.textField.text;
        _resetPswdData.password = _currentPswdView.textField.text;
        [_resetPswdData excuteHttpRequest];
        
        [self dismissKeyboard];
    } else {
        [SDAlertManagement showNormalErrorAlert:@"输入完整的信息"];
    }
   
}

#pragma mark UITableViewDelegate
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return 4;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    NSInteger row = [indexPath row];
    if (row == 0) {
        return 40;
    }
    return 45;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    NSInteger row = [indexPath row];
    
    if (row == 0) {
        UITableViewCell* cell = nil;
        static NSString* identifier = @"blankCell";
        cell = [tableView dequeueReusableCellWithIdentifier:identifier];
        if( cell == nil )
        {
            cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifier];
            cell.backgroundColor = [UIColor clearColor];
        }
        return cell;
    }else if (row == 1) {
        UITableViewCell* cell = nil;
        static NSString* identifier = @"oldPasswrodCell";
        cell = [tableView dequeueReusableCellWithIdentifier:identifier];
        if( cell == nil )
        {
            cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifier];
            UIView* separatorLine = [self cretateSeparatorLineView];
            [cell addSubview:separatorLine];
            _oldPswdView = [[SDLoginLTView alloc] initWithFrame:CGRectMake(0, separatorLine.bottom, self.view.width, 44) delegate:self];
            _oldPswdView.textField.secureTextEntry = YES;
            _oldPswdView.textField.autocorrectionType = UITextAutocorrectionTypeNo;
            _oldPswdView.textField.autocapitalizationType = UITextAutocapitalizationTypeNone;
            _oldPswdView.textField.returnKeyType = UIReturnKeyDone;
            _oldPswdView.textField.clearButtonMode = UITextFieldViewModeWhileEditing;
            [_oldPswdView setName:@"旧密码"];
            [cell addSubview:_oldPswdView];
        }
        return cell;
    }else if (row == 2) {
        UITableViewCell* cell = nil;
        static NSString* identifier = @"newPasswrodCell";
        cell = [tableView dequeueReusableCellWithIdentifier:identifier];
        if( cell == nil )
        {
            cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifier];
            UIView* separatorLine = [self cretateSeparatorLineView];
            separatorLine.frame = CGRectMake(20, 1, kScreenWidth-40, 1);
            [cell addSubview:separatorLine];
            _currentPswdView = [[SDLoginLTView alloc] initWithFrame:CGRectMake(0, separatorLine.bottom, self.view.width, 44) delegate:self];
            _currentPswdView.textField.secureTextEntry = YES;
            _currentPswdView.textField.autocorrectionType = UITextAutocorrectionTypeNo;
            _currentPswdView.textField.autocapitalizationType = UITextAutocapitalizationTypeNone;
            _currentPswdView.textField.returnKeyType = UIReturnKeyDone;
            _currentPswdView.textField.clearButtonMode = UITextFieldViewModeWhileEditing;
            [_currentPswdView setName:@"新密码"];
            [cell addSubview:_currentPswdView];
        }
        return cell;
    }
    else if (row == 3) {
        UITableViewCell* cell = nil;
        static NSString* identifier = @"confirmPasswrodCell";
        cell = [tableView dequeueReusableCellWithIdentifier:identifier];
        if( cell == nil )
        {
            cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifier];
            UIView* separatorLine = [self cretateSeparatorLineView];
            [cell addSubview:separatorLine];
            separatorLine.frame = CGRectMake(20, 1, kScreenWidth-40, 1);
             _confirmPswdView = [[SDLoginLTView alloc] initWithFrame:CGRectMake(0, separatorLine.bottom, self.view.width, 43) delegate:self];
            _confirmPswdView.textField.secureTextEntry = YES;
            _confirmPswdView.textField.autocorrectionType = UITextAutocorrectionTypeNo;
            _confirmPswdView.textField.autocapitalizationType = UITextAutocapitalizationTypeNone;
            _confirmPswdView.textField.returnKeyType = UIReturnKeyDone;
            _confirmPswdView.textField.clearButtonMode = UITextFieldViewModeWhileEditing;
            [_confirmPswdView setName:@"确认密码"];
            [cell addSubview:_confirmPswdView];
            separatorLine = [self cretateSeparatorLineView];
            separatorLine.top = _confirmPswdView.bottom;
            [cell addSubview:separatorLine];

        }
        return cell;
    }

    return nil;
}

#pragma mark - UIScrollView
- (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView {
    [self dismissKeyboard];
}

#pragma mark- SDFormBaseDataDelegate
-(void) willUploadOfFormData:(SDFormBaseData*) formdata
{
    [self showLoading];
}
-(void) formData:(SDFormBaseData*)formdata userInfo:(NSDictionary*) userInfo didFinishLoadWithError:(NSError*)error
{
    [self finishLoading];
    if (error == nil) {
        if ([formdata isKindOfClass:[SDResetPswdData class]])
        {
            [self onBack:nil];
        }

    } else {
        [SDAlertManagement showNormalErrorAlert:[error.userInfo objectForKey:@"SDHttpRequest_ResponseInfo"]];

    }
}
@end
